package com.easycarebn.main.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

import com.easycarebn.main.utils.IResult;
import com.easycarebn.main.utils.Utils;
import com.timbacsi.main.R;

public class FragmentLogin extends Fragment {

    private View mView;
    private static FragmentLogin fragment = null;
    private IResult callIResult;
    private static String mTitle;
    private EditText mEdtEmail;
    private TextView lblQuenmk;
    private EditText mEdtPwd;
    public static ProgressBar mLoadding;

    public static FragmentLogin newInstance(String title) {
            fragment = new FragmentLogin();
        mTitle = title;
        return fragment;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (activity instanceof IResult) {
            callIResult = (IResult) activity;
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.frangment_login, null);

        return mView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        callIResult.onDisplay(Utils.TASK_CHILD, "");
        Utils.setTitlePage(mView, mTitle);
        Utils.setOnTouchHideKeyboard(getActivity(), (ViewGroup) mView);
        mEdtEmail = (EditText) mView.findViewById(R.id.edEmail);
        mEdtPwd = (EditText) mView.findViewById(R.id.edPass);
        mEdtPwd.setOnEditorActionListener(new DoneOnEditorActionListener());

        mLoadding = (ProgressBar) mView.findViewById(R.id.loadding);
        lblQuenmk = (TextView) mView.findViewById(R.id.lblQuyenMk);
        Button btnLogin = (Button) mView.findViewById(R.id.btnLogin);
        btnLogin.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                checkCompleteRequest();
            }
        });

        lblQuenmk.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                callIResult.onDisplay(Utils.FORGET_PASS, getResources().getString(R.string.lblForgetpas_title));
            }
        });
        TextView create_account = (TextView) mView.findViewById(R.id.create_account);
        create_account.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                callIResult.onDisplay(Utils.CREATE_ACC, getResources().getString(R.string.lblTaotaikhoan)+"- ");
            }
        });

    }
    class DoneOnEditorActionListener implements OnEditorActionListener {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_DONE) {
                checkCompleteRequest(); 
                return true;
            }
            return false;
        }
    }
    private void checkCompleteRequest() {
        String email = mEdtEmail.getText().toString();
        String pwd = mEdtPwd.getText().toString();
//        if (TextUtils.isEmpty(email)) {
//            Utils.showMessage(getActivity(), R.string.ten_hien_thi_khong_duoc_bo_trong);
//            return;
//        }
        if (TextUtils.isEmpty(pwd)) {
            Utils.showMessage(getActivity(), R.string.mat_khau_khong_duoc_bo_trong);
            return;
        }
        
        if(pwd.contains(" ")){
        	  Utils.showMessage(getActivity(), R.string.mat_khau_khong_dc_chua_ki_tu_trong);
              return;
        }

        // Utils.disableEnableControls(false, (ViewGroup)mView);

        Utils.requestLogin(getActivity(), callIResult, email, pwd, true);
    }

}
